package com.xxgc.demo.job;

import cn.hutool.extra.spring.SpringUtil;
import com.xxgc.demo.controller.pojo.ImageRecyclingPOJO;
import com.xxgc.demo.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.File;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @Author: Sjy
 * @Date :2023/10/9 - 10 - 09 - 11:28
 * 图片回收
 */
@Slf4j
public class PictureRecyclingJob implements Job {
    //自动注入不会生效

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //通过hutool工具箱拿到spring的IOC注入
        RedisUtil redisUtil = SpringUtil.getBean("redisUtil");

        String key = "recycling_image";
        int i = 0;
        int j = 50;
        int k = j;
        while (true){
            log.info("目前正在判断"+i+"到"+j);
            List<Object> list = redisUtil.lGet(key, i, j);
            if(list.size() == 0){//没有垃圾图片
                break;
            }else{
                i += k;
                j += k;
            }
            //-------------------循环list判断是否过期------------
            for (Object o : list) {
                ImageRecyclingPOJO irPo = (ImageRecyclingPOJO) o;
                //上传图片的时间
                LocalDateTime date = irPo.getDate();
                //当前时间
                LocalDateTime now = LocalDateTime.now();

                long minutes = ChronoUnit.MINUTES.between(date, now);
                if(minutes >= 1){//超过1分钟还没提交表单
                    new File(irPo.getPath()).delete();
                    new File(irPo.getPath1()).delete();
                    //----------redis也移除-----------
                    redisUtil.lRemove(key,1,irPo);
                }
            }

        }
    }

}
